<template>
	<view class="full-page" v-if="totalPage">
		
	</view>
	<view v-else>
		<view class="wrap">
			<!-- <image class="bg_img" :src=" '/images/bg.png' | formatImgUrl" mode=""></image> -->
			<view class="head">

			</view>

			<view class="discount">
				<view class="tit">
					<view class="title">
						多选开票
						<text @tap="selectAll">全选</text>
					</view>
				</view>
				<view class="item display-flex just-between" v-for="(item,index) in list">
					
					<view class="leftbox display-flex">
						<image :src="'/images/successicon.png' | formatImgUrl" mode="aspectFit" class="cicon"
							v-if="item.checked" @tap.stop='selectItem(item)'></image>
						<image :src="'/images/circle.png' | formatImgUrl" mode="aspectFit" class="cicon" v-else
							@tap.stop='selectItem(item)'></image>
						
						<view class="display-flex" @tap="$go('/showcase/showcase/invoiceDetail?id='+item.id)">
							<image :src="item.w_pic | formatImgUrl" class="avatar"></image>
							<view class="leftbox_desc">
								<view class="view1">
									消费
								</view>
								<view class="view2">
									<block v-if="item.type == 1">现金充值粮币</block>
									<block v-if="item.type == 2">现金订单支出</block>
									<block v-if="item.type == 3">佣金充值粮币</block>
									<block v-if="item.type == 6">开通派单功能</block>
								</view>
								<view class="view3">
									<text>{{item.create_time}}</text>
							
								</view>
							</view>
						</view>
					</view>
					<view class="rightbox">
						<view class="num2">
							{{item.price}}
						</view>
						<view class="status2" v-if="item.f_status == 0">
							可开票
						</view>
						<view class="status2" v-if="item.f_status == 1">
							正在开票
						</view>
						<view class="status" v-if="item.f_status == 2">
							已开票
						</view>
						<view class="status3" v-if="item.f_status == 3">
							正在退票
						</view>
					</view>
				</view>
				<view class="empytView" v-if="list.length<=0">
					<image :src="'/images/kong.png' | formatImgUrl" mode="widthFix" />
				</view>
			</view>
		</view>

		<view class="dibox" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
			<view class="zaibox">
				<view class="zaileft">
					<view class="dbox">
						<view class="zaititle">
							<text>合并订单</text>
						</view>
						<view class="text">
							<text>共计{{total}}元待开票</text>
						</view>
					</view>
					<view class="zaitxt">
						<text>选择完成点击右侧“申请开票”进行后续操作</text>
					</view>
				</view>
				<view class="que" @click="submit()">
					<text>申请开票</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import * as db from '@/common/db.js'
	export default {
		data() {
			return {
				userInfo: this.$db.get('userInfo'),
				navList: [{
					id: 0,
					name: '可开票'
				}, {
					id: 1,
					name: '开票中'
				}, {
					id: 2,
					name: '已开票'
				}],
				navIndex: 2,
				list: [],
				datas: {},
				total: 0,
				ids: '',
				totalPage: true
			};
		},
		onReachBottom() {
			this.get_list();
		},
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止当前页面下拉刷新	
			}, 800);
			this.page = 1;
			this.list = [];
			this.get_list()
		},
		onLoad() {
			let auth = db.get('auth');
			console.log(auth, 777777777)
			//用户存在，不跳转，不存在直接跳转
			if (!auth) {
				// toLogin()
				uni.reLaunch({
					url: '/pages/login/login'
				})
				return;
			}

			this.page = 1;
			this.list = [];
			this.get_list()
		},
		onShow() {
			this.getDataInfo()
		},
		methods: {
			// 申请开票
			submit(){
				if(this.total <= 0 || !this.ids){
					this.$common.errorToShow('请选择开票订单')
					return 
				}
				
				this.$go('/showcase/showcase/openInvoice?id='+this.ids)
			},
			selectItem(item) {
				item.checked = !item.checked
				this.$forceUpdate()
				
				let countPrice = 0
				let ids= []
				this.list.forEach(item => {
					if(item.checked){
						countPrice += Number(item.price)
						ids.push(item.id)
					}
				})
				this.total = countPrice
				this.ids = ids.join(',')
			},
			// 全选
			selectAll() {
				let list = this.list
				let countPrice = 0
				let ids= []
				
				for (let i in list) {
					list[i].checked = true
					countPrice += Number(list[i].price)
					ids.push(list[i].id)
				}

				this.list = list
				this.total = countPrice
				this.ids = ids.join(',')
				this.$forceUpdate()
				console.log('selectAll', list);
			},
			// 多选
			checkboxChange(e) {
				// this.list[e].checked = !this.list[e].checked
			},
			checkboxGroupChange(e) {
				console.log(e);
			},
			getDataInfo() {
				var _this = this;

				this.$api.default.request('invoice/getInvoiceDea', {}).then((res) => {
					if (res.code) {
						_this.datas = res.data
					}
				})
			},
			qiehuan() {
				if (this.navIndex > 2) {
					this.navIndex = 1;
				} else {
					this.navIndex++
				}

				this.page = 1;
				this.list = [];
				this.get_list()
			},
			get_list() {
				var _this = this;

				this.$api.default.request('invoice/getList', {
					page: this.page,
					f_status: 0
				}).then((res) => {
					if (res.code) {

						if (res.data.data) {
							let list = res.data.data
							list.checked = false
							if (_this.list != null) {

								_this.list = _this.list.concat(list);

							} else {
								_this.list = list;
							}
							_this.page++; // 页数加一，加载下一页数据
						}
					}
					
					_this.totalPage = false
				})
			},
		}
	}
</script>

<style lang="scss">
	.wrap {
		width: 100%;
		height: 100vh;
		background: linear-gradient(180deg, #45C4B0 17%, #F2F5F2 57%);
		background-size: 100% 100%;
		position: relative;

		.bg_img {
			transform: rotate(10.5deg);
			position: absolute;
			left: 300rpx;
			top: 8rpx;
			z-index: 0;
		}

		.head {
			padding: 26rpx 30rpx;
			font-size: 32rpx;
			color: #fff;
			font-weight: 300;

			.tit {
				display: flex;
				align-items: center;

				image {
					width: 40rpx;
					height: 40rpx;
					object-fit: cover;
					margin-left: 8rpx;
				}

				.withdraw {
					width: 180rpx;
					height: 42rpx;
					border-top-left-radius: 25rpx;
					border-bottom-left-radius: 25rpx;
					background: rgba(255, 255, 255, 0.2);
					font-size: 24rpx;
					color: #FFFFFF;
					display: flex;
					align-items: center;
					position: absolute;
					right: 0;

					image {
						width: 26rpx;
						height: 26rpx;
						object-fit: cover;
						margin-left: 20rpx;
						margin-right: 4rpx;
					}
				}
			}

			.money_num {
				font-size: 60rpx;
				font-weight: bold;
				color: #FFFFFF;
			}

			.consumption {
				display: flex;
				margin-top: 40rpx;

				.today {
					margin-right: 80rpx;

					.total_tit {
						font-size: 24rpx;
						font-weight: 300;
						color: #FFFFFF;
					}

					.num {
						font-size: 28rpx;
						color: #FFFFFF;
					}

				}
			}
		}
		
		.cicon {
			width: 40rpx;
			height: 40rpx;
			margin-right: 20rpx;
		}
		.discount {
			margin: 0 30rpx 0;
			background: #fff;
			box-sizing: border-box;
			padding: 40rpx 30rpx;
			border-radius: 24rpx;

			.tit {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.title {
					font-size: 32rpx;
					font-weight: bold;
					color: #3D3D3D;

					.icon {
						width: 32rpx;
						height: 32rpx;
						margin-left: 8rpx;
						object-fit: cover;
					}

					text {
						font-family: '思源黑体';
						font-size: 12px;
						color: #45C4B0;
						margin-left: 16rpx;
					}
				}

				.tip {
					font-family: '思源黑体';
					font-size: 12px;
					color: #45C4B0;

				}
			}

			.item {
				width: 100%;
				position: relative;
				overflow: hidden;
				padding: 10rpx 0;
				border-bottom: 1px solid rgba(153, 153, 153, 0.1);

				.leftbox {
					width: 70%;
					font-family: Source Han Sans CN;
					font-size: 10px;
					color: #999999;

					.avatar {
						width: 80rpx;
						height: 80rpx;
						border-radius: 50%;
						margin-right: 8rpx;
					}

					.view1 {
						font-family: Source Han Sans CN;
						font-size: 14px;
						font-weight: 600;
						color: #3D3D3D;
					}

					.view2 {
						margin: 4rpx 0;
					}

				}

				.rightbox {
					text-align: right;

					.num {
						font-family: Source Han Sans CN;
						font-size: 18px;
						font-weight: bold;
						color: #45C4B0;
						margin-bottom: 4rpx;
					}

					.num2 {
						font-family: Source Han Sans CN;
						font-size: 18px;
						font-weight: bold;
						color: #3D3D3D;
						margin-bottom: 4rpx;
					}

					.status {
						font-family: Source Han Sans CN;
						font-size: 10px;
						color: #45C4B0;
					}

					.status2 {
						font-family: Source Han Sans CN;
						font-size: 10px;
						color: #999;
					}

					.status3 {
						font-family: Source Han Sans CN;
						font-size: 10px;
						color: #ED3232;
					}
				}
			}
		}
	}

	.dibox {
		z-index: 66;
		position: fixed;
		left: 0;
		bottom: 0;
		background-color: #ffffff;
		width: 100%;
		padding: 20rpx 0;
		box-shadow: 0px -8px 20px 0px rgba(69, 196, 176, 0.2);

		.zaibox {
			width: 630rpx;
			height: 80rpx;
			border-radius: 12px;
			background: #3D3D3D;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 0 auto 10rpx;

			.zaileft {
				padding: 0 20rpx;

				.dbox {
					display: flex;
					align-items: center;

					.zaititle {

						font-size: 28rpx;
						font-weight: bold;
						background: linear-gradient(293deg, #45C4B0 -5%, #56E8B5 128%);
						-webkit-background-clip: text;
						-webkit-text-fill-color: transparent;
						background-clip: text;
						text-fill-color: transparent;

					}

					.text {
						font-size: 28rpx;
						font-weight: bold;
						color: #FFFFFF;
						padding-left: 8rpx;
					}
				}

				.zaitxt {
					font-size: 20rpx;
					font-weight: normal;
					letter-spacing: 0px;
					color: #999999;
				}
			}

			.que {
				width: 190rpx;
				height: 80rpx;
				border-radius: 10px;
				opacity: 1;
				background: #45C4B0;
				font-size: 24rpx;
				font-weight: bold;
				color: #FFFFFF;
				text-align: center;
				line-height: 80rpx;
			}
		}

		.xiebox {
			display: flex;
			align-items: center;
			justify-content: center;

			.xietxt {
				font-size: 20rpx;
				font-weight: bold;
				color: #999999;

				text {
					font-size: 20rpx;
					font-weight: bold;
					color: #45C4B0;
					padding: 0 10rpx;
				}
			}
		}
	}
</style>